//
//  DrawerViewController.m
//  QuanNengSaoMiaoZhuShou
//
//  Created by lanou3g on 16/3/10.
//  Copyright © 2016年 于博文. All rights reserved.
//

#import "DrawerViewController.h"

@interface DrawerViewController ()<UIGestureRecognizerDelegate>

@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //如果没有手势
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        [self.tap setEnabled:NO];
        self.tap.delegate = self;
        [self.view addGestureRecognizer:self.tap];
    }
    //边缘清扫,拉出菜单
    if (!self.Edge) {
        self.Edge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(openMeun)];
        self.Edge.edges = UIRectEdgeLeft;
        self.Edge.delegate = self ;
        [self.view addGestureRecognizer:self.Edge];
    }
    self.view.backgroundColor = [UIColor grayColor];
    // Do any additional setup after loading the view.
}

//设置中间视图控制器
-(void)setRootViewController:(UIViewController *)viewController title:(NSString *)title{
    //如果传进来的viewcontroller 是空的直接返回
    if (!viewController) {
        return;
    }
    //设置中间视图控制器
    [self.rootVC.view removeFromSuperview];
    self.rootVC = viewController;
    [self addChildViewController:viewController];
    if (self.tap.enabled) {
        viewController.view.frame = CGRectMake(65*kSCreamWidth/100, 0, kSCreamWidth, kSCreamHeight);
    }
    
    [self.view addSubview:viewController.view];
     //关闭抽屉
    [self showRootViewController:nil];
     //设置导航栏
    [self setNaButton:title];
    
}
//展示中间视图的方法（关闭抽屉)
-(void)showRootViewController:(UIViewController *)viewcontroller{
    
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(0, 0, kSCreamWidth, kSCreamHeight);
     }];
    //手势关闭、
    //rootView交互打开
 
    //手势打开
    [self.tap setEnabled:NO];
    //rootview禁用
    self.rootVC.view.userInteractionEnabled = YES;
    
    
}
//打开抽屉
-(void)showleftViewController:(UIViewController *)leftViewController{
     if (![self.view.subviews containsObject:self.leftVC.view]) {
        //插入抽屉的view
        [self.view insertSubview:self.leftVC.view atIndex:0];
     }
     //动画移动根视图控制器
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(65*kSCreamWidth/100, 0, kSCreamWidth, kSCreamHeight);
    }];
    
    //手势打开
    [self.tap setEnabled:YES];
    //rootview禁用
    self.rootVC.view.userInteractionEnabled = YES;
    
    
}

#pragma mark ----------左按钮-中间按钮----------------
//设置左上角按钮
-(void)setNaButton:(NSString *)title {
    
    UINavigationController *na = (UINavigationController *)self.rootVC;
     UIViewController *vc = na.viewControllers[0];
    
    //设置title上的视图
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCreamWidth*25/100, kSCreamWidth *10/100)];
    UIButton *titleButton =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, kSCreamWidth*25/100, kSCreamWidth *6/100)];

    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake((CGRectGetMaxX(titleButton.frame)/2-kSCreamWidth*5/200), CGRectGetMaxY(titleButton.frame)+3,  kSCreamWidth*5/100, kSCreamWidth*3/100)];

    [titleView addSubview:titleButton];
    [titleView addSubview:imgView];
    [titleButton setTitle:title forState:(UIControlStateNormal)];
    [titleButton setFont:[UIFont systemFontOfSize:23]];
    imgView.image = [UIImage imageNamed:@"arrow_down_wirt"];
    vc.navigationItem.titleView = titleView;
    [na.navigationBar addSubview:titleView];
    
    vc.navigationItem.titleView = titleView;
     na.navigationBar.tintColor = [UIColor whiteColor];
    //设置左按钮
    vc.navigationItem.leftBarButtonItem = ({
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ic_drawer_am"]  style:UIBarButtonItemStylePlain target:self action:@selector(openMeun)];
         item;
    });
}

//打开抽屉
-(void)openMeun{
    [self showleftViewController:nil];
    [self.tap setEnabled:YES];
     self.rootVC.view.userInteractionEnabled = NO;
}


//tapAction
-(void)tapAction{
    //关闭抽屉
    [self showRootViewController:nil];
    [self.tap setEnabled:NO];
    [self.Edge setEnabled:YES];
    self.rootVC.view.userInteractionEnabled = YES;
}

//delegate
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer == self.tap) {
        return CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:_rootVC.view]);
    }
    return YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
